package com.scuola.scuoladocente;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class operations {

	public static String wsCall(String wsMethod, LinkedHashMap<String,String> hm){
		try{
			String ws_ip = LoginActivity.sharedPreferences.getString("WS_IP", "").toString();
			final String NAMESPACE = "http://scuola.com";
			final String URL = "http://"+ws_ip+":8080/ScuolaWS/services/WebService?wsdl";
			final String SOAP_ACTION = "http://scuola.com/"+wsMethod;
			final String METHOD_NAME = wsMethod;
			
        	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        	
        	/* Parameters used to invoke a webService method  */
        	Iterator i = hm.entrySet().iterator();
        	while(i.hasNext()){
        		Map.Entry entry = (Map.Entry)i.next();
        		String key = entry.getKey().toString();
        		String value = entry.getValue().toString();
        		request.addProperty(key, value);
        	}
        	
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
			return response.toString();
		}catch(Exception e){
			e.printStackTrace();
			return "ServerNotFound";
		}
	}
}
